<?php

namespace addons\qingdong\library;

class AES
{
    private $key;
    private $iv;

    public function __construct($key, $iv) {
        $this->key = $key;
        $this->iv = $iv;
    }

    public function encrypt($data) {
        // 使用 AES-256-CBC 加密
        $encrypted = openssl_encrypt($data, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
        // 返回 base64 编码的加密结果
        return base64_encode($encrypted);
    }

    public function decrypt($encryptedData) {
        // 解码 base64
        $encryptedData = base64_decode($encryptedData);
        // 使用 AES-256-CBC 解密
        return openssl_decrypt($encryptedData, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
    }
}